package com.tanhua.dubbo.api;

import com.tanhua.model.mongo.UserLike;
import org.apache.dubbo.config.annotation.DubboService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Sort;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.data.mongodb.core.query.Update;

import java.util.List;

@DubboService
public class UserLikeApiImpl implements UserLikeApi{
    @Autowired
    private MongoTemplate mongoTemplate;

    @Override
    public Boolean saveOrUpdate(Long userId, Long likeUserId, boolean b) {
        try {
            Query query = new Query();
            query.addCriteria(Criteria
                    .where("userId").is(userId)
                    .and("likeUserId").is(likeUserId));
            //查询是否已经存在
            UserLike userLike = mongoTemplate.findOne(query, UserLike.class);
            //不存在保存
            if (userLike == null) {
                userLike = new UserLike();
                userLike.setLikeUserId(likeUserId);
                userLike.setIsLike(b);
                userLike.setUserId(userId);
                userLike.setCreated(System.currentTimeMillis());
                userLike.setUpdated(System.currentTimeMillis());
                mongoTemplate.save(userLike);
            }else {
                //存在，更新
                Update update = new Update();
                update.set("isLike", b);
                update.set("updated", System.currentTimeMillis());
                mongoTemplate.updateFirst(query, update, UserLike.class);
            }
            return true;
        } catch (Exception e) {
            e.printStackTrace();
            return false;
        }


    }

    @Override
    public List<UserLike> findById(Long userId, Boolean isLike, Integer page, Integer pagesize) {
        if (isLike == null) {
            isLike = true;
        }
        Query query = new Query();
        if (page != null) {
            query.addCriteria(Criteria.where("userId").is(userId).and("isLike").is(isLike))
                    .with(Sort.by(Sort.Direction.DESC, "updated"))
                    .with(PageRequest.of(page - 1, pagesize));
        } else {
            query.addCriteria(Criteria.where("userId").is(userId).and("isLike").is(isLike))
                    .with(Sort.by(Sort.Direction.DESC, "updated"));
        }
        List<UserLike> userLikes = mongoTemplate.find(query, UserLike.class);
        return userLikes;
    }

    @Override
    public List<UserLike> findFans(Long userId, Integer page, Integer pagesize) {
        Query query = new Query();
        if (page != null) {
            query.addCriteria(Criteria.where("likeUserId").is(userId).and("isLike").is(true))
                    .with(Sort.by(Sort.Direction.DESC, "updated"))
                    .with(PageRequest.of(page - 1, pagesize));
        }else {
            query.addCriteria(Criteria.where("likeUserId").is(userId).and("isLike").is(true))
                    .with(Sort.by(Sort.Direction.DESC, "updated"));
        }
        List<UserLike> userLikes = mongoTemplate.find(query, UserLike.class);
        return userLikes;
    }
}
